package org.example.main.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.main.entity.Admin;
import org.example.main.entity.Landlord;
import org.example.main.utils.CommonResult;
import org.example.main.entity.Tenant;
import org.example.main.service.TenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@Tag(name = "租客管理",description = "租客管理相关接口")
@RestController
@RequestMapping("/tenant")
public class TenantController {

    @Autowired
    private TenantService tenantService;

    @Operation(summary = "租客注册")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "注册成功"),
            @ApiResponse(responseCode = "400", description = "用户名已存在"),
            @ApiResponse(responseCode = "500", description = "注册失败"),
    })
    @PostMapping("/register")//http://localhost:8089/main/tenant/register
    public CommonResult register(@RequestBody Tenant tenant) {
        CommonResult commonResult = new CommonResult();
        try{
            Tenant existing = tenantService.findByUsername(tenant.getUsername());
            if (existing != null) {
                commonResult.setCode(400);
                commonResult.setMsg("用户名已存在");
            }else {
                tenantService.add(tenant);
                commonResult.setCode(200);
                commonResult.setMsg("注册成功");
                Tenant currentTenant = tenantService.findByUsername(tenant.getUsername());
                commonResult.setData(currentTenant);
            }
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("注册失败");
        }
        return commonResult;
    }

    @Operation(summary = "租客登录")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "登录成功"),
            @ApiResponse(responseCode = "401", description = "用户名或密码错误"),
            @ApiResponse(responseCode = "200", description = "登录失败")
    })
    @PostMapping("/login")//http://localhost:8089/main/tenant/login
    public CommonResult login(@RequestBody Tenant tenant) {
        CommonResult commonResult = new CommonResult();
        try{
            Tenant real = tenantService.findByUsername(tenant.getUsername());
            if (real == null || !real.getPassword().equals(tenant.getPassword())) {
                commonResult.setCode(401);
                commonResult.setMsg("用户名或密码错误");
            } else {
                commonResult.setCode(200);
                commonResult.setMsg("登录成功");
                commonResult.setData(real);
            }
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("登陆失败");
        }
        return commonResult;
    }

    @Operation(summary = "修改租客记录")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "修改成功"),
            @ApiResponse(responseCode = "500",description = "修改失败")
    })
    //修改数据
    @PutMapping("/updateById")//http://localhost:8089/main/tenant/updateById
    public CommonResult updateById(@RequestBody Tenant tenant) {
        CommonResult commonResult = new CommonResult();
        try {
            tenantService.updateById(tenant);
            commonResult.setCode(200);
            commonResult.setMsg("修改成功");
        }
        catch (Exception e) {
            commonResult.setCode(500);
            commonResult.setMsg("修改失败"+e.getMessage());
        }
        return commonResult;
    }

    @Operation(summary = "删除租客记录")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "删除成功"),
            @ApiResponse(responseCode = "500",description = "删除失败")
    })
    @DeleteMapping("/delete/{tenantId}")//http://localhost:8089/main/tenant/delete/{tenantId}
    public CommonResult deleteById(@PathVariable long tenantId) {
        CommonResult commonResult = new CommonResult();
        try{
            tenantService.deleteById(tenantId);
            commonResult.setCode(200);
            commonResult.setMsg("删除成功");
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("删除失败");
        }
        return commonResult;
    }

    @Operation(summary = "查询所有")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @GetMapping("/findAll")//http://localhost:8089/main/tenant/findAll
    public CommonResult findAll() {
        CommonResult commonResult = new CommonResult();
        try{
            List<Tenant> tenants = tenantService.findAll();
            commonResult.setCode(200);
            commonResult.setMsg("查询成功");
            commonResult.setData(tenants);
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("查询失败");
        }
        return commonResult;
    }

    @Operation(summary = "按id查询")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "查询成功"),
            @ApiResponse(responseCode = "500",description = "查询失败"),
            @ApiResponse(responseCode = "404",description = "未查询到")
    })
    @GetMapping("/findById/{tenantId}")//http://localhost:8089/main/tenant/findById/{tenantId}
    public CommonResult findById(@PathVariable long tenantId) {
        CommonResult commonResult = new CommonResult();
        try{
            Tenant tenant=tenantService.findById(tenantId);
            if(tenant!=null){
                commonResult.setCode(200);
                commonResult.setMsg("查询成功");
                commonResult.setData(tenant);
            }
            else{
                commonResult.setCode(404);
                commonResult.setMsg("未查询到");
            }
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("查询失败");
        }
        return commonResult;
    }
}

